﻿#define _CRT_SECURE_NO_WARNINGS 1;
#include "Sort.h"

//打印arr
void arrprintf(int* arr,int n)
{
	for (int i = 0; i < n; i++)
	{
		printf("%d ", arr[i]);
	}
	printf("\n");
}

//直接插入排序
void InsertSort(int* arr, int n)
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = arr[end + 1];
		while (end>=0)
		{
			if (tmp < arr[end])
			{
				arr[end + 1] = arr[end];
				end--;
			}
			else
			{
				break;
			}
		}
		arr[end + 1] = tmp;
	}
}


//希尔排序
void ShellSort(int* arr, int n)
{
	int gap = n;
	while (gap > 1)
	{
		gap = gap / 3 + 1;
		for (int i = 0; i < n - gap; i++)
		{
			int end = i;
			int tmp = arr[end + gap];
			while (end >= 0)
			{
				if (tmp < arr[end])
				{
					arr[end + gap] = arr[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			arr[end + gap] = tmp;
		}
	}
}

void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}

//冒泡排序
void BubbleSort(int* arr, int n)
{
	
	for (int i = 0; i < n - 1; i++)
	{
		int flag = 1;//排序完成
		for (int j = 0; j < n - i - 1; j++)
		{
			if (arr[j+1] < arr[j])
			{
				Swap(&arr[j], &arr[j + 1]);
				flag = 0;
			}
		}
		if (flag == 1)
			return;
	}
}

//直接选择排序
void SelectSort(int* arr, int n)
{
	int begin = 0;
	int end = n-1;
	while (begin < end)
	{
		int min = begin;
		int max = begin;
		for (int i = begin; i <= end; i++)
		{
			if (arr[min] > arr[i])
			{
				min = i;
			}
			if (arr[max] < arr[i])
			{
				max = i;
			}	
		}
		if (max == begin)
		{
			max = min;
		}
		Swap(&arr[min], &arr[begin]);
		Swap(&arr[min], &arr[end]);
		begin++;
		end--;
	}
}

//向下调整算法
void AdjustDown(int* arr, int parent, int n)
{
	int child = parent * 2 + 1; //左孩子
	while (child < n)
	{
		if (child + 1 < n && arr[child] < arr[child+1])
		{
			child++;
		}

		if (arr[parent] < arr[child])
		{
			Swap(&arr[child], &arr[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}

//堆排序
void HeapSort(int* arr, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(arr, i, n);
	}

	int end = n - 1;
	while (end > 0)
	{
		Swap(&arr[0], &arr[end]);
		AdjustDown(arr, 0, end);
		end--;
	}
}

//lomuto前后指针法
int _QuickSort_2(int* arr,int left,int right)
{
	int keyi = left;
	int prev = left, cur = prev + 1;
	while (cur <= right)
	{
		if (arr[cur] < arr[keyi] && ++prev != cur)
		{
			Swap(&arr[prev], &arr[cur]);
		}
		cur++;
	}
	Swap(&arr[keyi], &arr[prev]);
	return prev;
	
}

int _QuickSort_1(int* arr, int left, int right)
{
	int keyi = left;
	left++;
	while (left <= right)
	{
		while (left <= right && arr[right] > arr[keyi])
		{
			right--;
		}
		while (left <= right && arr[left] < arr[keyi])
		{
			left++;
		}
		if (left <= right)
		{
			Swap(&arr[left++], &arr[right--]);
		}
	}
	Swap(&arr[keyi], &arr[right]);
	return right;
}

//快速排序
void QuickSort(int* arr, int left,int right)
{
	
	if (left >= right)
	{
		return;
	}

	int keyi = _QuickSort_2(arr, left, right);
	QuickSort(arr, left, keyi - 1);
	QuickSort(arr, keyi + 1, right);
}

//归并排序
void _MergeSort(int* a, int left, int right, int* tmp)
{
	if (left >= right)
	{
		return;
	}
	int mid = (right + left) / 2;
	//[left,mid] [mid+1,right]
	_MergeSort(a, left, mid, tmp);
	_MergeSort(a, mid + 1, right, tmp);
	int begin1 = left, end1 = mid;
	int begin2 = mid + 1, end2 = right;
	int index = begin1;
		//合并两个有序数组为⼀个数组
		while (begin1 <= end1 && begin2 <= end2)
		{
			if (a[begin1] < a[begin2])
			{
				tmp[index++] = a[begin1++];
			}
			else
			{
				tmp[index++] = a[begin2++];
			}
		}
	while (begin1 <= end1)
	{
		tmp[index++] = a[begin1++];
	}
	while (begin2 <= end2)
	{
		tmp[index++] = a[begin2++];
	}
	for (int i = left; i <= right; i++)
	{
		a[i] = tmp[i];
	}
}

void MergeSort(int* a, int n)
{
	int* temp = (int*)malloc(sizeof(int) * n);
	if (temp == NULL)
	{
		perror("malloc fail!");
		exit(1);
	}
	int* tmp = temp;
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
	tmp = NULL;
}

//非比较排序
void CountSort(int* arr, int n)
{
	int max_num = arr[0];
	int min_num = arr[0];
	for (int i = 0; i < n; i++)
	{
		if (max_num < arr[i])
		{
			max_num = arr[i];
		}
		if (min_num > arr[i])
		{
			min_num = arr[i];
		}
	}
	int num = max_num - min_num + 1;
	int* temp = (int*)malloc(sizeof(int) * num);
	if (temp == NULL)
	{
		printf("CountSort error\n");
		perror("malloc fail");
		exit(1);
	}
	int* tmp = temp;
	for (int i = 0; i < num; i++)
	{
		tmp[i] = 0;
	}


	for (int i = 0; i < n; i++)
	{
		(tmp[arr[i]-min_num])++;
	}

	int index = 0;
	for (int i = 0; i < num; i++)
	{
		while (tmp[i])
		{
			arr[index++] = (i+min_num);
			tmp[i]--;
		}
	}

	free(temp);
	temp = NULL;
}